package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.request.UserDataPermissionRequest;
import com.xh.bussiness.system.constant.UserDataCacheConstant;
import com.xh.bussiness.system.model.UserDataPermission;
import com.xh.bussiness.system.dao.UserDataPermissionMapper;
import com.xh.bussiness.system.service.IUserDataPermissionService;
import com.xh.bussiness.tiku.constant.BookChapterConstant;
import com.xh.core.service.IElasticSearchService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.CacheUtils;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 用户数据权限 服务实现类
 * </p>
 *
 * @author Pitt
 * @since 2020-11-23
 */
@Service
public class UserDataPermissionServiceImpl extends BaseServiceImpl<UserDataPermissionMapper, UserDataPermission> implements IUserDataPermissionService {

    @Autowired
    private IElasticSearchService elasticSearchService;

    @Override
    public XhPage queryUserDataPermissionPage(XhPage page, UserDataPermissionRequest bean) {
        QueryWrapper qw = new QueryWrapper<UserDataPermission>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page, qw);
    }

    @Override
    public List queryUserDataPermission(UserDataPermissionRequest bean) {
        QueryWrapper qw = new QueryWrapper<UserDataPermission>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public UserDataPermission queryByUserIdAndSubjectCode(String userId, String subjectCode) {
        QueryWrapper<UserDataPermission> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("subject_code", subjectCode);
        List<UserDataPermission> userDataPermissions = this.dao.selectList(qw);
        if (CollectionUtils.isEmpty(userDataPermissions)) {
            return null;
        }
        return userDataPermissions.get(0);
    }

    @Override
    public boolean saveUserDataPermission(UserDataPermissionRequest request) {
        String userId = request.getUserId();
        this.dao.deleteByUserId(request.getUserId());
        if (!CollectionUtils.isEmpty(request.getDatas())) {
            for (UserDataPermission data : request.getDatas()) {
                data.setUserId(request.getUserId());
                this.dao.insert(data);
            }
        }
        CacheUtils.delete(UserDataCacheConstant.USER_DATA_VERSIN_KEY_PREFIX + userId);
        // 删除es 中用户的bookversion 缓存数据

        if (!CollectionUtils.isEmpty(request.getDatas())) {
            for (UserDataPermission data : request.getDatas()) {
                String id = data.getSubjectCode() + "-" + data.getUserId();
                String index = BookChapterConstant.BOOK_CHAPTER_TREE_INDEX_PREFIX + data.getSubjectCode();
                String type = BookChapterConstant.BOOK_CHAPTER_TREE_TYPE_PREFIX + data.getSubjectCode();
                elasticSearchService.deleteDocument(id, index, type);
            }
        }
        return true;
    }


    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(UserDataPermissionRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getUserId())) {
            qw.eq("user_id", bean.getUserId());
        }
        return qw;
    }
}
